package com.hou.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hou.commonutils.R;
import com.hou.eduservice.entity.EduChapter;
import com.hou.eduservice.entity.EduVideo;
import com.hou.eduservice.entity.chapter.ChapterVo;
import com.hou.eduservice.entity.chapter.VideoVo;
import com.hou.eduservice.mapper.EduChapterMapper;
import com.hou.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hou.eduservice.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-10-17
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService service;

    // 获取章节带小节
    @Override
    public List<ChapterVo> getChapterVideoById(String courseId) {

        // 1通过课程id查询所有章节
        QueryWrapper<EduChapter> eduChapterQueryWrapper = new QueryWrapper<>();
        eduChapterQueryWrapper.eq("course_id",courseId);
        List<EduChapter> chapterList = baseMapper.selectList(eduChapterQueryWrapper);

        // 2通过课程id查询所有小节
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("course_id",courseId);
        List<EduVideo> videoList = service.list(eduVideoQueryWrapper);

        // 将章节带小节类实例化
        ArrayList<ChapterVo> list = new ArrayList<>();

        // 3遍历封装章节
        for (int i = 0; i < chapterList.size(); i++) {
            EduChapter eduChapter = chapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);

            ArrayList<VideoVo> videos = new ArrayList<>();

            // 4遍历根据id值封装小节
            for (int j = 0; j < videoList.size(); j++) {
                EduVideo eduVideo = videoList.get(j);
                // 判断小节chapterid与章节id是否相同，相同加入集合
                if (chapterVo.getId().equals(eduVideo.getChapterId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videos.add(videoVo);
                }
            }

            chapterVo.setChildren(videos);

            list.add(chapterVo);
        }


        // 5返回结果
        return list;
    }

    // 删除章节
    // 如果里面有小节需要先将小节删除再将章节删除
    @Override
    public Boolean deleteChapter(String chapterId) {

        // 小节构造器查询小节
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("chapter_id",chapterId);

        // 获取小节
        int count = service.count(eduVideoQueryWrapper);
        // 小节存在删除失败
        if (count > 0) {
            return false;
        } else {
            baseMapper.deleteById(chapterId);
            return true;
        }

    }

    @Override
    public void deleteChapterById(String id) {
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();

        chapterQueryWrapper.eq("course_id",id);

        baseMapper.delete(chapterQueryWrapper);
    }

}
